// @ts-nocheck
import fetch from "@blueos.network.fetch"
import { getDeviceId } from './device.js'


const host = 'http://121.37.46.189:8888'


export const identify = (id, data) => {
  return new Promise((resolve, reject) => {
    const deviceID = getDeviceId()
    fetch.fetch({
      url: `${host}/v1/object_identify`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 600,
      data: {
        id: id,
        data: data,
        uid: deviceID,
      },
      success: (res) => {
        if (res && res.data && res.data.data) {
          console.log(res.data.data);
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        console.log('fail', data, code)
        reject()
      }
    })
  })
}

/**
 * 画图
 * @param {number} id 
 * @param {any} data 
 * @returns 
 */
export const draw = (id, data, style) => {
  return new Promise((resolve, reject) => {
    const deviceID = getDeviceId()
    fetch.fetch({
      url: `${host}/v1/draw`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 600,
      data: {
        id: id,
        data: data,
        uid: deviceID,
        style,
      },
      success: (res) => {
        if (res && res.data && res.data.data && res.data.data.results) {
          console.log(res.data.data.results);
          resolve(res.data.data.results)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        console.log('fail', data, code)
        reject()
      }
    })
  })
}



export const deleteImage = (id) => {
  return new Promise((resolve, reject) => {
    const deviceID = getDeviceId()
    fetch.fetch({
      url: `${host}/v1/delete_image`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 600,
      data: {
        id: id,
        uid: deviceID
      },
      success: (res) => {
        if (res && res.data) {
          console.log(res.data);
          resolve(res.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        console.log('fail', data, code)
        reject()
      }
    })
  })
}

/**
 * 查询保存的画画列表
 * @returns 
 */
export const listImages = () => {
  return new Promise((resolve, reject) => {
    const deviceID = getDeviceId()
    // console.log("=================")
    fetch.fetch({
      url: `${host}/v1/list_images?uid=${deviceID}`,
      method: 'get',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 600,
      success: (res) => {
        // console.log("======", res)
        if (res && res.data && res.data.data) {
          // console.log(res.data.data);
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        console.log('fail', data, code)
        reject()
      }
    })
  })
}

/**
 * 设置壁纸
 * @param {string} id 
 * @returns 
 */
export const setWallpaper = (id) => {
  return new Promise((resolve, reject) => {
    const deviceID = getDeviceId()
    fetch.fetch({
      url: `${host}/v1/set_wallpaper`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 600,
      data: {
        id: id,
        uid: deviceID,
        is_wallpaper: true
      },
      success: () => {
        resolve()
      },
      fail: (data, code) => {
        console.log('fail', data, code)
        reject()
      }
    })
  })
}

/**
 * 保存图片
 * @param {string} imageName 
 * @returns 
 */
export const saveImage = (imageName) => {
  return new Promise((resolve, reject) => {
    const deviceID = getDeviceId()
    fetch.fetch({
      url: `${host}/v1/save_image`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 600,
      data: {
        save_path: imageName,
        uid: deviceID
      },
      success: () => {
        resolve()
      },
      fail: (data, code) => {
        console.log('fail', data, code)
        reject()
      }
    })
  })
}
